package com.wlcorp.forcazamis.web.action.stock;

import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.sql.Date;
import java.util.Calendar;
import java.util.Iterator;
import java.util.List;

import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.poi.hssf.usermodel.HSSFClientAnchor;
import org.apache.poi.hssf.usermodel.HSSFPatriarch;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.util.Region;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;

import com.wlcorp.forcazamis.business.stock.IStockService;
import com.wlcorp.forcazamis.domain.baseinfo.Customer;
import com.wlcorp.forcazamis.domain.baseinfo.Factory;
import com.wlcorp.forcazamis.domain.stock.Stock;
import com.wlcorp.forcazamis.domain.stock.StockProduct;
import com.wlcorp.forcazamis.domain.stock.StockProductDetail;
import com.wlcorp.forcazamis.domain.system.Company;
import com.wlcorp.forcazamis.domain.system.Staff;
import com.wlcorp.forcazamis.sys.util.AppConstant;
import com.wlcorp.forcazamis.sys.util.CommonUtil;
import com.wlcorp.forcazamis.sys.util.ContextUtil;
import com.wlcorp.forcazamis.sys.util.DateUtil;
import com.wlcorp.forcazamis.sys.util.Pagination;
import com.wlcorp.forcazamis.sys.util.SessionValue;
import com.wlcorp.forcazamis.sys.util.WebUtil;
import com.wlcorp.forcazamis.sys.util.XLSFactory;

public class StockAction extends DispatchAction {
	public ActionForward stockManager(ActionMapping mapping,ActionForm form,
			HttpServletRequest request,HttpServletResponse response){
//		List stockList = ContextUtil.getInstance().getStockService().getStockList(null);
//		request.setAttribute("stockList",stockList);
		return mapping.findForward("manager");
	}
	
	public ActionForward getStockList(ActionMapping mapping,ActionForm form,
			HttpServletRequest request,HttpServletResponse response){
		Staff currentStaff = SessionValue.getInstance(request).getStaff();
		currentStaff = ContextUtil.getInstance().getStaffService().getStaffById(currentStaff.getStaffId());
		Stock stock = new Stock();
		WebUtil.setRequestToObject(request,stock);
		 
		IStockService service = ContextUtil.getInstance().getStockService();
		int page = 1;
		int count = 0;
	    if (null != request.getParameter(Pagination.PAGE_ATTRIBUTE)) {
	      page = Integer.parseInt(request.getParameter(Pagination.
	          PAGE_ATTRIBUTE));
	    }
	    count = (int)service.getStockCount(stock).longValue();
	    Pagination pagination = new Pagination();
	    pagination.setPageCount(Pagination.PAGE_RECORD_LINE_NUM);
	    pagination.generatePageJSPFragment(request, count);
	    stock.setPage(page, Pagination.PAGE_RECORD_LINE_NUM);
	    System.out.println(stock.getStockCode());
		List<Stock> stockList = service.getStockList(stock);
		for (Iterator<Stock> it = stockList.iterator(); it.hasNext();) {
			while (it.hasNext()) {
				Stock stock1=(Stock)it.next();
				Customer customer=ContextUtil.getInstance().getCustomerService().getCustomerById(stock1.getCustomerId());
				if ((null==currentStaff.getCustomers()||currentStaff.getCustomers().size()==0)&&!currentStaff.getRole().getRoleId().equals(new Long(1))&&!currentStaff.getRole().getRoleId().equals(new Long(11))) {
					it.remove();
				}
				if (false==currentStaff.getCustomers().isEmpty()&&!currentStaff.getCustomers().contains(customer)&&!currentStaff.getRole().getRoleId().equals(new Long(1))&&!currentStaff.getRole().getRoleId().equals(new Long(11))) {
					it.remove();
				}
			}
		}
		
		request.setAttribute("stockList",stockList);
		return mapping.findForward("list");
	}
	
	public ActionForward getStock(ActionMapping mapping,ActionForm form,
			HttpServletRequest request,HttpServletResponse response){
		Stock stock = new Stock();
		WebUtil.setRequestToObject(request,stock);
		stock = ContextUtil.getInstance().getStockService().getStockById(stock.getStockId());
		request.setAttribute("factory",stock.getFactory());
		request.setAttribute("stock",stock);
		return mapping.findForward("modify");
	}
	
	public ActionForward modifyStock(ActionMapping mapping,ActionForm form,
			HttpServletRequest request,HttpServletResponse response){
		Stock stock = new Stock();
		WebUtil.setRequestToObject(request,stock);
		
		Staff currentStaff = SessionValue.getInstance(request).getStaff();
		currentStaff = ContextUtil.getInstance().getStaffService().getStaffById(currentStaff.getStaffId());
	 
		Stock stockTmp = ContextUtil.getInstance().getStockService().getStockById(stock.getStockId());
		stockTmp.setModifier(currentStaff);
		stockTmp.setModifiedDate(new Date(Calendar.getInstance().getTimeInMillis()));
		stockTmp.setOtherPromise(stock.getOtherPromise());
		stockTmp.setQualityRequire(stock.getQualityRequire());
		stockTmp.setFittingRequire(stock.getFittingRequire());
		stockTmp.setMemo(stock.getMemo());
		stockTmp.setDeliveryDate(stock.getDeliveryDate());
		stockTmp.setSupplierLinkman(stock.getSupplierLinkman());
		stockTmp.setStockLevel(stock.getStockLevel());
		stockTmp.setStockType(stock.getStockType());
		
		ContextUtil.getInstance().getStockService().saveStock(stockTmp);
		if(AppConstant.BASE_BEAN_STATUS_ERROR == stock.getStatus()){
			WebUtil.setAlertMessage(request,stock.getMsg());
		}else{
			WebUtil.setAlertMessage(request,"保存成功！");
		}
		
		Stock stock2 = ContextUtil.getInstance().getStockService().getStockById(stockTmp.getStockId());
		
		request.setAttribute("stock",stock2);
		request.setAttribute("factory",stock2.getFactory());
		return mapping.findForward("modify");
	}
	
	public ActionForward deleteStock(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		Stock stock = new Stock();
		WebUtil.setRequestToObject(request, stock);
		IStockService service = ContextUtil.getInstance().getStockService();
		stock = service.getStockById(stock.getStockId());
		if (null != stock) {
			service.deleteStock(stock);
			if (AppConstant.BASE_BEAN_STATUS_ERROR == stock.getStatus()) {
				WebUtil.setAlertMessage(request, stock.getMsg());
			}else{
				WebUtil.setAlertMessage(request,"删除成功！");
			}
		}
		return getStockList(mapping, form, request, response);
	}
	
	public ActionForward getStockProductList(ActionMapping mapping,ActionForm form,
			HttpServletRequest request,HttpServletResponse response){
		Long stockId = Long.valueOf(request.getParameter("stockId"));
		IStockService stockService=ContextUtil.getInstance().getStockService();
		Stock stock = stockService.getStockById(stockId);
		StockProduct stockProduct = new StockProduct();
		stockProduct.setStock(stock);
		List<StockProduct> stockProductList = stockService.getStockProductListByStockId(stockId);
		List<Factory> factoryList = ContextUtil.getInstance().getFactoryService().getFactoryList(null);
		request.setAttribute("stock",stock);
		request.setAttribute("stockProductList",stockProductList);
		request.setAttribute("factoryList",factoryList);
		return mapping.findForward("stockProductList");
		
	}
	
	public ActionForward modifyStockProduct(ActionMapping mapping,ActionForm form,
			HttpServletRequest request,HttpServletResponse response){
		String id=request.getParameter("id").toString();
		Long productId=Long.parseLong(request.getParameter("productId"));
		String factoryCode=request.getParameter("factoryCode"+id);
		String figureSize=request.getParameter("figureSize"+id);
		String orderSize=request.getParameter("orderSize"+id);
		String customerBrand=request.getParameter("customerBrand"+id);
		String windowChange=request.getParameter("windowChange"+id);
		String noseChange=request.getParameter("noseChange"+id);
		String level=request.getParameter("level"+id);
		String shelfCruraLength=request.getParameter("shelfCruraLength"+id);
		String shelfCruraWidth=request.getParameter("shelfCruraWidth"+id);
		String frameType=request.getParameter("frameType"+id);
		String ectorhinal=request.getParameter("ectorhinal"+id);
		String cover=request.getParameter("cover"+id);
		
		String headRadian=request.getParameter("headRadian"+id);
		String headLength=request.getParameter("headLength"+id);
		String windowSilk=request.getParameter("windowSilk"+id);
		String crura=request.getParameter("crura"+id);
		String framePitch=request.getParameter("framePitch"+id);
		String frameCrankle=request.getParameter("frameCrankle"+id);
		
		String shelfCrura=request.getParameter("shelfCrura"+id);
		String headExplain=request.getParameter("headExplain"+id);
		String productExplain=request.getParameter("productExplain"+id);
		String notice=request.getParameter("notice"+id);
		String spec=request.getParameter("spec");
		StockProduct stockProduct=new StockProduct();
		stockProduct=ContextUtil.getInstance().getStockService().getStockProductById(productId);
		stockProduct.setFactoryCode(factoryCode);
		stockProduct.setFigureSize(figureSize);
		stockProduct.setOrderSize(orderSize);
		stockProduct.setCustomerBrand(customerBrand);
		stockProduct.setWindowChange(windowChange);
		stockProduct.setNoseChange(noseChange);
		stockProduct.setLevel(level);
		stockProduct.setShelfCruraLength(shelfCruraLength);
		stockProduct.setShelfCruraWidth(shelfCruraWidth);
		stockProduct.setFrameType(Integer.parseInt(frameType));
		stockProduct.setEctorhinal(ectorhinal);
		stockProduct.setHeadRadian(headRadian);
		stockProduct.setHeadLength(headLength);
		stockProduct.setWindowSilk(windowSilk);
		stockProduct.setCrura(crura);
		stockProduct.setFramePitch(framePitch);
		stockProduct.setFrameCrankle(frameCrankle);
		stockProduct.setCover(cover);
		stockProduct.setShelfCrura(shelfCrura);
		stockProduct.setHeadExplain(headExplain);
		stockProduct.setProductExplain(productExplain);
		stockProduct.setNotice(notice);
		stockProduct.setSpecialize(spec);
		ContextUtil.getInstance().getStockService().updateStockProduct(stockProduct);
		
		if(AppConstant.BASE_BEAN_STATUS_ERROR == stockProduct.getStatus()){
			WebUtil.setAlertMessage(request,stockProduct.getMsg());
		}else{
			WebUtil.setAlertMessage(request,"修改成功！");
		}
		 
		request.setAttribute("stockId", request.getParameter("stockId"));
		return getStockProductList(mapping,form,request,response);
	}
	
	public ActionForward deleteStockProduct(ActionMapping mapping,ActionForm form,
			HttpServletRequest request,HttpServletResponse response){
		IStockService stockService=ContextUtil.getInstance().getStockService();
		StockProduct stockProduct=new StockProduct();
		stockProduct=stockService.getStockProductById(Long.parseLong(request.getParameter("productId")));
		long nums=0;
		float prices=0;
		if (null != stockProduct.getStockProductDetailSet()&& stockProduct.getStockProductDetailSet().size() > 0) {
			for (Iterator iter = stockProduct.getStockProductDetailSet().iterator(); iter.hasNext();) {
				StockProductDetail stockProductDetail = (StockProductDetail) iter.next();
				if (stockProductDetail.isDeleteFlag() == true) {
					continue;
				}
				if(null!=stockProductDetail.getAmount()&&!stockProductDetail.getAmount().equals("")){
					nums+=stockProductDetail.getAmount();
					if(null!=stockProductDetail.getUnitPrice()&&!stockProductDetail.getUnitPrice().equals("")){
						prices+=stockProductDetail.getUnitPrice()*stockProductDetail.getAmount();
					}
				}
			}
		}
		Stock stock=stockService.getStockById(Long.parseLong(request.getParameter("stockId").toString()));
		stock.setTotalGlasses(stock.getTotalGlasses()-(int)nums);
		stock.setTotalPrice(stock.getTotalPrice()-prices);
		stockService.updateStock(stock);
		
		stockService.deleteStockProduct(stockProduct);
		if(AppConstant.BASE_BEAN_STATUS_ERROR == stockProduct.getStatus()){
			WebUtil.setAlertMessage(request,stockProduct.getMsg());
		}else{
			WebUtil.setAlertMessage(request,"删除成功！");
		}
		request.setAttribute("stockId", request.getParameter("stockId"));
		return getStockProductList(mapping,form,request,response);
	}
	
	public ActionForward modifyStockProductDetail(ActionMapping mapping,ActionForm form,
			HttpServletRequest request,HttpServletResponse response){
		IStockService stockService=ContextUtil.getInstance().getStockService();
		StockProductDetail detail=stockService.getStockProductDetailById(Long.parseLong(request.getParameter("productDetailId")));
		
//		WebUtil.setResponseNoCache(response);
		StockProductDetail stockProductDetail = new StockProductDetail();
		StockProduct stockProduct=new StockProduct();
		stockProduct.setProductId(Long.parseLong(request.getParameter("productId")));
		stockProductDetail.setStockProduct(stockProduct);
		WebUtil.setRequestToObject(request,stockProductDetail);
		
		stockProductDetail.setLetteringCode(WebUtil.toGb(stockProductDetail.getLetteringCode()));
		stockProductDetail.setPiColor(WebUtil.toGb(stockProductDetail.getPiColor()));
		stockProductDetail.setLetteringColor(WebUtil.toGb(stockProductDetail.getLetteringColor()));
		stockProductDetail.setLetteringSize(WebUtil.toGb(stockProductDetail.getLetteringSize()));
		stockProductDetail.setShelfSurefaceColor(WebUtil.toGb(stockProductDetail.getShelfSurefaceColor()));
		stockProductDetail.setShelfCruraColor(WebUtil.toGb(stockProductDetail.getShelfCruraColor()));
		stockProductDetail.setMemo(WebUtil.toGb(stockProductDetail.getMemo()));
		stockProductDetail.setPlatingFactoryName(WebUtil.toGb(stockProductDetail.getPlatingFactoryName()));
		
		Stock stock=stockService.getStockById(Long.parseLong(request.getParameter("stockId").toString()));
		int totoalGlasses=stock.getTotalGlasses().intValue()-detail.getAmount().intValue();
		float totalPrice=stock.getTotalPrice()-detail.getUnitPrice()*detail.getAmount();
		
		stock.setTotalGlasses(totoalGlasses+stockProductDetail.getAmount().intValue());
		stock.setTotalPrice(totalPrice+stockProductDetail.getUnitPrice()*stockProductDetail.getAmount());
		stockService.saveStock(stock);
		stockService.saveStockProductDetail(stockProductDetail);
		
		String productDetailId=request.getParameter("productDetailId").toString();
		String s="<a href='saveStockProductDetail("+request.getParameter("productDetailId").toString()+",document.getElementById("+productDetailId+"))'>保存</a>";
		response.setBufferSize(10240);
		WebUtil.printText(response,s);
//		WebUtil.printText(response,request.getParameter("productDetailId").toString());
		//<a href="javascript:saveStockProductDetail(<%=i%>,document.getElementById('<%=stockProductDetail.getProductDetailId()%>'))">保存</a>
		return null;
	}
	
	public ActionForward uploadProductImage(ActionMapping mapping,ActionForm form,
			HttpServletRequest request,HttpServletResponse response){
		System.out.println("stockManager ajax");
		WebUtil.printText(response,"图片上传成功！");
		return null;
	}
	
	public ActionForward deleteProductImage(ActionMapping mapping,ActionForm form,
			HttpServletRequest request,HttpServletResponse response){
		StockProduct stockProduct = new StockProduct();
		stockProduct=ContextUtil.getInstance().getStockService().getStockProductById(Long.parseLong(request.getParameter("productId")));
		stockProduct.setProductImage("");
		ContextUtil.getInstance().getStockService().updateStockProduct(stockProduct);
		request.setAttribute("stockId", request.getParameter("stockId"));
		if(AppConstant.BASE_BEAN_STATUS_ERROR == stockProduct.getStatus()){
			WebUtil.setAlertMessage(request,stockProduct.getMsg());
		}else{
			WebUtil.setAlertMessage(request,"删除成功！");
		}
		return getStockProductList(mapping,form,request,response);
	}
	
	public ActionForward printStock(ActionMapping mapping,ActionForm form,
			HttpServletRequest request,HttpServletResponse response){
		Long stockId = Long.valueOf(request.getParameter("stockId"));
		IStockService stockService=ContextUtil.getInstance().getStockService();
		Stock stock = stockService.getStockById(stockId);
		StockProduct stockProduct = new StockProduct();
		stockProduct.setStock(stock);
		List<StockProduct> stockProductList = stockService.getStockProductListByStockId(stockId);
		
		Company company=ContextUtil.getInstance().getCompanyService().getCompany();
		request.setAttribute("stock",stock);
		request.setAttribute("stockProductList",stockProductList);
		request.setAttribute("company", company);
		return mapping.findForward("print");
		
	}
	
 
	public ActionForward drawbackStock(ActionMapping mapping,ActionForm form,
			HttpServletRequest request,HttpServletResponse response){
		WebUtil.setResponseNoCache(response);
		Long stockId = Long.valueOf(request.getParameter("stockId"));
		Stock stock=ContextUtil.getInstance().getStockService().getStockById(stockId);

		ContextUtil.getInstance().getStockService().drawbackStock(stock);
		WebUtil.printText(response,"操作成功！");
		return null;
	}
 
	@SuppressWarnings({ "deprecation", "unchecked" })
	public ActionForward exportStock(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		Stock stock = new Stock();
		WebUtil.setRequestToObject(request, stock);
		StockProduct stockProduct = new StockProduct();
		stockProduct.setProductId(stock.getStockId());
		List<StockProduct> stockProductList = ContextUtil.getInstance().getStockService().getStockProductListByStockId(stock.getStockId());
		stock = ContextUtil.getInstance().getStockService().getStockById(stock.getStockId());
		Company company=ContextUtil.getInstance().getCompanyService().getCompany();
		String staffName=SessionValue.getInstance(request).getStaff().getStaffName();

		//取出模板
		String excelPath = request.getSession().getServletContext().getRealPath("")+"/WEB-INF/STOCK_TEMPLATE.xls";
		XLSFactory xls=XLSFactory.getInstance();
		xls.setXLS(response, "STOCK_"+DateUtil.currentDate()+".xls");
		HSSFWorkbook wb=xls.getXLSTemplate1(excelPath);
		
		HSSFSheet sheet1=wb.getSheetAt(0);
		xls.setSheet(sheet1);
		
		xls.getRow(0);
		xls.setCellExist(0, company.getCfullName()+"生产定单");
		 
		xls.getRow(2);
		xls.setCellExist(1, CommonUtil.nullToStr(stock.getStockCode()));
		xls.setCellExist(4, CommonUtil.nullToStr(stock.getTotalGlasses()));
		xls.setCellExist(6, CommonUtil.nullToStr(stock.getTotalGlasses()));
		
		xls.getRow(3);
		xls.setCellExist(1, staffName);
		xls.setCellExist(4, CommonUtil.nullToStr(stock.getFactory().getFactoryFullName()));
		xls.setCellExist(6, CommonUtil.nullToStr(stock.getDeliveryDate()));
		
		xls.getRow(4);
		xls.setCellExist(1, "");
		xls.setCellExist(4, CommonUtil.nullToStr(stock.getTotalPrice()));
		xls.setCellExist(6, CommonUtil.nullToStr(stock.getStockDate()));
		
		xls.getRow(27);
		xls.setCellExist(5, CommonUtil.nullToStr(company.getCshortName())+"签字：");
		
		xls.getRow(33);
		xls.setCellExist(1, CommonUtil.nullToStr(stock.getQualityRequire()));
		
		xls.getRow(40);
		xls.setCellExist(1, CommonUtil.nullToStr(stock.getFittingRequire()));
		 
		if (null != stockProductList){
			for (int i = 0; i < stockProductList.size(); i++) {
				StockProduct product=(StockProduct)stockProductList.get(i);
				HSSFSheet sheeti=wb.createSheet("第"+(i+1)+"款");
				xls.setSheet(sheeti);
				
				xls.createRow(0);
				xls.getSheet().addMergedRegion(new Region(0,(short)(0),0,(short)(8)));
				xls.setCellCreate(0, CommonUtil.nullToStr("【"+stock.getStockCode()+" "+stock.getStockLevelName())+"】"+" "+"第 "+i+1+" 款 / "+"共 "+stockProductList.size()+" 款");
				xls.setCellStyleBold(xls.getCell());
				
				xls.createRow(2);
				xls.getSheet().addMergedRegion(new Region(2,(short)(0),2,(short)(8)));
				xls.setCellCreate(0, product.getHeadExplain());
				
				xls.createRow(3);
				xls.setCellCreate(0, "我司型号：");
				xls.setCellCreate(1, CommonUtil.nullToStr(product.getMyCode()));
				xls.setCellCreate(2, "附图尺寸：");
				xls.setCellCreate(3, CommonUtil.nullToStr(product.getFigureSize()));
				xls.setCellCreate(4, "窗型更改：");
				xls.setCellCreate(5, " ");
				
				xls.createRow(4);
				xls.setCellCreate(0, "客户品牌：");
				xls.setCellCreate(1, "");
				xls.setCellCreate(2, "订单尺寸：");
				xls.setCellCreate(3, CommonUtil.nullToStr(product.getOrderSize()));
				xls.setCellCreate(4, "鼻梁更改：");
				xls.setCellCreate(5, " ");
				
				xls.createRow(5);
				xls.setCellCreate(2, "生产档次：");
				xls.setCellCreate(3, CommonUtil.nullToStr(product.getLevel()));
				
				xls.createRow(6);
				xls.setCellCreate(0, "客户型号");
				xls.setCellStyleBackground(xls.getCell());
				xls.setCellCreate(1, "印字颜色");
				xls.setCellStyleBackground(xls.getCell());
				xls.setCellCreate(2, "印字尺寸");
				xls.setCellStyleBackground(xls.getCell());
				xls.getSheet().addMergedRegion(new Region(6,(short)(3),6,(short)(4)));//合并单元格
				xls.setCellCreate(3, "电镀颜色");
				xls.setCellStyleBackground(xls.getCell());
				xls.setCellCreate(5, "脚套颜色");
				xls.setCellStyleBackground(xls.getCell());
				xls.setCellCreate(6, "备注");
				xls.setCellStyleBackground(xls.getCell());
				xls.setCellCreate(7, "副数");
				xls.setCellStyleBackground(xls.getCell());
				xls.setCellCreate(8, "单价");
				xls.setCellStyleBackground(xls.getCell());
				 
				long nums=0;
				int prices=0;
				int m=0;
				if (null != stockProduct.getStockProductDetailSet()&& stockProduct.getStockProductDetailSet().size() > 0) {
					for (Iterator iter = stockProduct.getStockProductDetailSet().iterator(); iter.hasNext();) {
						StockProductDetail productDetail = (StockProductDetail) iter.next();
						if (productDetail.isDeleteFlag() == true) {
							continue;
						}
						if(null!=productDetail.getAmount()&&productDetail.getAmount().equals("")){
							nums+=productDetail.getAmount();
							if(null!=productDetail.getUnitPrice()&&productDetail.getUnitPrice().equals("")){
								prices+=productDetail.getUnitPrice()*productDetail.getAmount();
							}
						}
						m+=m;
						xls.createRow(7+m);
						xls.setCellCreate(0, CommonUtil.nullToStr(productDetail.getLetteringCode()));
						xls.setCellCreate(1, CommonUtil.nullToStr(productDetail.getLetteringColor()));
						xls.setCellCreate(2, CommonUtil.nullToStr(productDetail.getLetteringSize()));
						xls.setCellCreate(3, "");
						xls.setCellCreate(5, "");
						xls.setCellCreate(6, CommonUtil.nullToStr(productDetail.getMemo()));
						xls.setCellCreate(7, CommonUtil.nullToStr(productDetail.getAmount()));
						xls.setCellCreate(8, CommonUtil.nullToStr(productDetail.getUnitPrice()));
						
					}
				}
				xls.createRow(7+m);
				xls.setCellCreate(5, "合计副数：");
				xls.getSheet().addMergedRegion(new Region(7+m,(short)(6),7+m,(short)(8)));
				xls.setCellCreate(3, nums);
				
				xls.createRow(8+m);
				xls.setCellCreate(5, "合计价格（含税）：");
				xls.getSheet().addMergedRegion(new Region(7+m,(short)(0),8+m,(short)(4)));
				xls.setCellCreate(3, nums+"("+prices+")");
					
				xls.createRow(9+m);
				xls.setCellCreate(0, "镜腿：");
				xls.setCellCreate(1, CommonUtil.nullToStr(stockProduct.getShelfCrura()));
				xls.setCellCreate(6, "镜腿长度：");
				xls.setCellCreate(7, CommonUtil.nullToStr(stockProduct.getShelfCruraLength()));

				xls.createRow(10+m);
				xls.setCellCreate(0, "装头弧度：");
				xls.setCellCreate(1, CommonUtil.nullToStr(stockProduct.getHeadRadian()));
				xls.setCellCreate(3, "装头长度：");
				xls.setCellCreate(4, CommonUtil.nullToStr(stockProduct.getHeadLength()));
				xls.setCellCreate(6, "镜架宽度：");
				xls.setCellCreate(7, CommonUtil.nullToStr(stockProduct.getShelfCruraWidth()));

				xls.createRow(11+m);
				xls.setCellCreate(0, "窗丝：");
				xls.setCellCreate(1, CommonUtil.nullToStr(stockProduct.getWindowSilk()));
				xls.setCellCreate(3, "鼻托：");
				xls.setCellCreate(4, CommonUtil.nullToStr(stockProduct.getEctorhinal()));
				xls.setCellCreate(6, "脚套形状：");
				xls.setCellCreate(7, CommonUtil.nullToStr(stockProduct.getShelfCruraWidth()));
				
				xls.createRow(12+m);
				xls.setCellCreate(0, "镜腿螺丝：");
				xls.setCellCreate(1, CommonUtil.nullToStr(stockProduct.getCrura()));
				xls.setCellCreate(3, "镜架弯度：");
				xls.setCellCreate(4, CommonUtil.nullToStr(stockProduct.getFramePitch()));
				xls.setCellCreate(6, "镜架斜度：");
				xls.setCellCreate(7, CommonUtil.nullToStr(stockProduct.getShelfCruraWidth()));

				xls.createRow(14+m);
				xls.setCellCreate(0, "注意："+CommonUtil.nullToStr(stockProduct.getNotice()));
				 
				xls.createRow(15+m);
				xls.setCellCreate(0, CommonUtil.nullToStr(stockProduct.getNotice()));

				if(null!=stockProduct.getSpecialize()&&stockProduct.getSpecialize().contains(new String("F"))){
					xls.createRow(16+m);
					xls.setCellCreate(0, "该款有新配件，请注意");
				}
				if(null!=stockProduct.getSpecialize()&&stockProduct.getSpecialize().contains(new String("C"))){
					xls.createRow(17+m);
					xls.setCellCreate(0, "该款有新颜色，请注意");
				}
				if(null!=stockProduct.getSpecialize()&&stockProduct.getSpecialize().contains(new String("S"))){
					xls.createRow(18+m);
					xls.setCellCreate(0, "该款有新结构，请注意");
				}
				if (null != stockProduct.getProductImage()&& !stockProduct.getProductImage().equals("")) {
					ByteArrayOutputStream byteArrayOut = new ByteArrayOutputStream();
					BufferedImage bufferImg;
					try {
						bufferImg = ImageIO.read(new File(request.getRealPath("")+product.getProductImage()));
						ImageIO.write(bufferImg, "jpg", byteArrayOut);
					} catch (IOException e) {
						e.printStackTrace();
					}
					HSSFPatriarch patriarch = sheeti.createDrawingPatriarch(); 
					HSSFClientAnchor anchor = new HSSFClientAnchor(19 + m, 0, 512, 255,(short) 1, 1, (short) 10, 20);
					anchor.setAnchorType(2);
					patriarch.createPicture(anchor, wb.addPicture(byteArrayOut.toByteArray(), HSSFWorkbook.PICTURE_TYPE_JPEG));
				}    
			}
		}
		
		xls.exportXLS(response);
		return null;
	}
	
	public ActionForward orderManager(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		return mapping.findForward("orderManager");
	}
	public ActionForward queryOrder(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		Object startDate=request.getParameter("startDate");
		Object endDate=request.getParameter("endDate");
		
		Staff currentStaff = SessionValue.getInstance(request).getStaff();
		currentStaff = ContextUtil.getInstance().getStaffService().getStaffById(currentStaff.getStaffId());
		StockProductDetail detail = new StockProductDetail();
		WebUtil.setRequestToObject(request,detail);
		 
//		if(null!=startDate&&!startDate.equals("null")){
//			detail.setStartDate(startDate.toString());
//		}
//		if(null!=endDate&&!endDate.equals("null")){
//			detail.setEndDate(endDate.toString());
//		}
		IStockService service = ContextUtil.getInstance().getStockService();
		List detailList=service.getListByCondition(detail);
		int page = 1;
		int count = 0;
	    if (null != request.getParameter(Pagination.PAGE_ATTRIBUTE)) {
	      page = Integer.parseInt(request.getParameter(Pagination.PAGE_ATTRIBUTE));
	    }
//	    count = (int)service.getStockProductDetailCount(detail).longValue();
	    count = detailList.size();
	    Pagination pagination = new Pagination();
	    pagination.setPageCount(Pagination.PAGE_RECORD_LINE_NUM);
	    pagination.generatePageJSPFragment(request, count);
	    detail.setPage(page, Pagination.PAGE_RECORD_LINE_NUM);
	    
//		List<Stock> stockList = service.getStockList(stock);
//		for (Iterator<Stock> it = stockList.iterator(); it.hasNext();) {
//			while (it.hasNext()) {
//				Stock stock1=(Stock)it.next();
//				Customer customer=ContextUtil.getInstance().getCustomerService().getCustomerById(stock1.getCustomerId());
//				if ((null==currentStaff.getCustomers()||currentStaff.getCustomers().size()==0)&&!currentStaff.getRole().getRoleId().equals(new Long(1))&&!currentStaff.getRole().getRoleId().equals(new Long(11))) {
//					it.remove();
//				}
//				if (false==currentStaff.getCustomers().isEmpty()&&!currentStaff.getCustomers().contains(customer)&&!currentStaff.getRole().getRoleId().equals(new Long(1))&&!currentStaff.getRole().getRoleId().equals(new Long(11))) {
//					it.remove();
//				}
//			}
//		}
		
		request.setAttribute("detailList",detailList);
		request.setAttribute("startDate",startDate);
		request.setAttribute("endDate",endDate);
		return mapping.findForward("orderList");
	}
	
}
